<?php
  //1、实现数据库链接    mysqli_connect打开一新的数据库
  // $link=mysqli_connect('127.0.0.1', 'root', 'root', 'p301');
  // //检测数据库是否链接成功
  // if(!$link){
  //    echo '<script> alert("您的数据库链接失败。请检查")</script>';die;
  // }
  // //$link数据库对象 mysqli_query运行数据命令
  //    mysqli_query($link,'set names utf8');
  //include_once 'init.php';

function getpages($biao,$cur_page,$per_page,$page_num = 5){
  $offset_page = floor($page_num / 2); 
  //当前访问的A标签页码
 // echo $offset_page;die;
  $star = $cur_page - $offset_page;


  //开始
  $end = $cur_page + $offset_page;



  //利用翻页计算偏移量的公式求出limit的第一个参数值
  $sql = "select count(*) as 'zongshu' from `$biao` ";
  //数数据库当中全部的数据有多少条  71
  $result = mysqli_query($GLOBALS['link'] , $sql);

  // $total_num = selectone($GLOBALS['c'],$file=count('*'));


  //运行存入结果  71
  $total_num = mysqli_fetch_assoc($result)['zongshu'];
  //总页码数


  $total_page = ceil($total_num / $per_page);  
  //计算当前总页数
 // echo  $per_page;die; 

  //结束

 //echo $total_page;
 //echo $total_num;
// echo $per_page;die;
   
  if($end>$total_page){


    $end=$total_page; 
    $star=$total_page-$page_num+1;
  }
  if ($star < 1) {
      $star = 1; 
      $end=$page_num;
  }
// echo $total_page;die;
   if($total_page<$page_num){
   $end=$total_page;
    

   }
if($end==$total_page){
  

}


 //加载页面
  $pages = '';
//禁止
  $disabled= $cur_page==1?'disabled':'';

//A标签跳转
  $href=$cur_page==1?'#':'index.php?c='.$GLOBALS['c'].'&a='.$GLOBALS['a'] .'&admin=1';
//首页
  $pages .= '<li class="'.$disabled.'"><a href="'.$href.'">首页</a></li>';
//上一页
  $prev =$cur_page-1;//当前页减一
  //如果当前页唯一的话则不能进行跳转。否则跳转到当前页减一的页面
  $prev_href =$cur_page==1?'#':'index.php?c='.$GLOBALS['c'].'&a='.$GLOBALS['a'] .'&admin=1&p=d'.$prev;

  $pages .= '<li class="'.$disabled.'"><a href="'.$prev_href.'">上一页</a></li>';
//下一页




  for ($i = $star; $i <= $end
    ; $i++) {
      if ($i == $cur_page) {
          $pages .= '<li><a style="background-color:blue">'. $i . '</a></li>';
      } else {
          $pages .= '<li><a href="index.php?c='.$GLOBALS['c'].'&a='.$GLOBALS['a'] .'&admin=1&p=' . $i . '">' . $i . '</a></li>';
      }
  }




  $disabled= $cur_page==$total_page?'disabled':'';
  //当前页jia一
  $next =$cur_page+1;
//如果当前页w为一的话则不能进行跳转。否则跳转到当前页jia一的页面
  $next_href =$cur_page==$total_page?'#':'index.php?c='.$GLOBALS['c'].'&a='.$GLOBALS['a'].'&admin=1&p='.$next;

  $pages .= '<li class="'.$disabled.'"><a href="'.$next_href.'">下一页</a></li>';

  $last_href=$cur_page==$total_page?'#':'index.php?c='.$GLOBALS['c'].'&a='.$GLOBALS['a'].'&admin=1&p='.$total_page;

   $pages .= '<li class="'.$disabled.'"><a href="'.$last_href.'">末页</a></li>';

  return $pages;
}
 